//
//  KKPurchaseOrderItemList.m
//  FreightAssistant
//
//  Created by Mac mini on 15/12/16.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "KKPurchaseOrderItemList.h"

@implementation KKPurchaseOrderItemList

- (void)appendItemsWithArray:(NSArray *)itemArray {
    
    [super appendItemsWithArray: itemArray withItemClass: [KKPurchaseOrderItem class]];
}

@end

#pragma mark -
#pragma mark - KKPurchaseOrderItem methods

@interface KKPurchaseOrderItem() {
    
    NSString *_orderDescription;
}

@end

@implementation KKPurchaseOrderItem

- (double)totalAmountOfCommodityItems {
    
    double amount = 0;
    for (KKPurchaseCommodityItem *item in _kk_purchaseCommodityItems) {
        amount += [item totalAmount];
    }
    
    return amount;
}

#pragma mark -

- (NSString *)orderDescription {
    
    if (_orderDescription == nil) {
        
        NSInteger commodityTotalCount = 0;
        
        for (KKPurchaseCommodityItem *item in _kk_purchaseCommodityItems) {
            commodityTotalCount += [[item kk_number] integerValue];
        }
        
        _orderDescription = [NSString stringWithFormat: @"共%@种%@件商品",@(_kk_purchaseCommodityItems.count),@(commodityTotalCount)];
    }
    
    return _orderDescription;
}

#pragma mark - Setter methods

- (void)setKk_customerItem:(NSDictionary *)kk_customerItem {
    
    _kk_customerItem = [KKCustomerItem new];
    [_kk_customerItem kk_parserJsonDictionary: kk_customerItem];
}

- (void)setKk_logisticsItem:(NSDictionary *)kk_logisticsItem {
    
    _kk_logisticsItem = [KKLogisticsItem new];
    [_kk_logisticsItem kk_parserJsonDictionary: kk_logisticsItem];
    
    self.kk_logisticsAmount = kk_logisticsItem[@"amount"];
    self.kk_logisticsNumber = kk_logisticsItem[@"number"];
}

- (void)setKk_purchaseCommodityItems:(NSArray *)kk_purchaseCommodityItems {
    
    NSMutableArray *items = [NSMutableArray array];
    
    for (NSDictionary *itemDictionary in kk_purchaseCommodityItems) {
        
        KKPurchaseCommodityItem *commodityItem = [KKPurchaseCommodityItem new];
        [commodityItem kk_parserJsonDictionary: itemDictionary];
        [items addObject: commodityItem];
    }
    _kk_purchaseCommodityItems = [NSArray arrayWithArray: items];
}

@end